﻿using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.Data;
using System.Threading;
using System.Windows.Threading;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{

    public partial class ViewModelBase : NotifyBase
    {
        public void RaiseMessage(MessageInfo info)
        {  
            RaiseMessage(new MessageEventArgs(info));
        }


        /// <summary>
        /// Internal function to raise an event
        /// </summary>
        /// <param name="type"></param>
        public void RaiseMessage(MessageEventArgs args)
        { 
            if (CheckAccess())
            {
                if (OnMessage != null)
                    OnMessage(this, args);
            }
            else
                Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart)delegate
                {
                    if (OnMessage != null)
                        OnMessage(this, args);
                });
        }

        public void RaiseMessage(string message)
        {  
            if (CheckAccess())
            {
                MessageEventArgs args = new MessageEventArgs(message);

                if (OnMessage != null)
                    OnMessage(this, args);
            }
            else
                Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart)delegate
                {
                    MessageEventArgs args = new MessageEventArgs(message);

                    if (OnMessage != null)
                        OnMessage(this, args);
                });
        }
        public ViewModelBase()
        {
            SetUp();
        }
        public event EventHandler<MessageEventArgs> OnMessage;
        public virtual void SetUp()
        {
            PM = new Dictionary<string, object>();
            PM["RUNID"] = this.RUNID;
            ORDERNUM = "";
            IsOnNew = false;
            Helper.GetDate(ref date, ref year, ref month, ref day);
            NextPageCommand = new DelegateCommand(x => NextPage());
            PrePageCommand = new DelegateCommand(x => PrePage());
        }

        public virtual void LoadEnv()
        {
        }

        public virtual DataSet Edit(DataRow dwInput)
        {
            return null;
        }
        public virtual DataSet Query(DataRow dwInput)
        {
            return null;
        }
        public virtual DataSet Delete(DataRow dwInput)
        {
            return null;
        }
        public virtual DataSet Save(DataRow dwInput)
        {
            return null;
        }
        public virtual DataSet Copy(DataRow dwInput)
        {
            return null;
        }

        DataRow ChgSrc = null;
        public virtual void BatchPropertyChange(DataRow _ChgSrc)
        {
            if (ChgSrc == _ChgSrc && _ChgSrc == null)
                return;
            ChgSrc = _ChgSrc;
            foreach (DataColumn cm in TBMain.Columns)
            {
                OnPropertyChanged(cm.ColumnName);
            }
        }

        public string CommdandInsert = "";
        public virtual void POSTCHG()
        {
            if (CommdandInsert.Length > 0)
            {
                BATCHINSERT();
            }
        }

        public virtual void BATCHINSERT()
        {
            CommdandInsert = "INSERT ALL " + CommdandInsert + " SELECT * FROM DUAL";
            or1 = OracleDB.CreateCommand();
            or1.CommandText = CommdandInsert;
            CommdandInsert = "";
            Connect2Proxy(or1);
        }
        public virtual void BATCHWB14(DataRow _dwWB14)
        {
            CommdandInsert += BATCHINSERT(_dwWB14);
            if (CommdandInsert.Length > 8000)
            {
                BATCHINSERT();
            }
        }
        public virtual string BATCHINSERT(DataRow dwData)
        {
            string value = "";
            return value;
        } 
        public virtual void OnAppModeChg()
        {

        }
    }

    public class ITXDateTime
    {
        int year = 2012, month = 1, day = 1, hh = 0, mm = 0, ss = 0;
        private DateTime m_StartDateTime = new DateTime();
        public DateTime StartDateTime
        {
            get
            {
                if (StartDate == "0")
                    return m_StartDateTime;

                Helper.SplitDate(ref year, ref month, ref day, StartDate);
                Helper.SplitTime(ref hh, ref mm, StartTime);
                m_StartDateTime = new DateTime(year, month, day, hh, mm, 0);
                return m_StartDateTime;
            }
            set
            {
                m_StartDateTime = value;
            }
        }
        private DateTime m_EndDateTime = new DateTime();
        public DateTime EndDateTime
        {
            get
            {
                if (EndDate == "0")
                    return m_EndDateTime;

                Helper.SplitDate(ref year, ref month, ref day, EndDate);
                Helper.SplitTime(ref hh, ref mm, EndTime);
                m_EndDateTime = new DateTime(year, month, day, hh, mm, 0);
                return m_EndDateTime;
            }
            set
            {
                m_EndDateTime = value;
            }
        }

        public string StartTime
        {
            get;
            set;
        }
        public string StartDate
        {
            get;
            set;
        }
        public double ProcessTime
        {
            get;
            set;
        }
        public string EndTime
        {
            get;
            set;
        }
        public string EndDate
        {
            get;
            set;
        }
        public int BLOCKS
        {
            get;
            set;
        }
        public ITXDateTime()
        {

        }
    }
}
